********************************************************************************
** 	TITLE:		ca2019_ces_contact_networks                                   ** 	
**	DATA:       Mongrain_Module                                               **
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATE:		October 2022 					                              **	
**	VERSION:	Stata 16					                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "ca2019_ces_contact_networks", replace

* Open dataset

use "Mongrain_Module.dta", clear


*****************************
** DISTRICT-LEVEL OUTCOMES **
*****************************

* Note: 1 = Liberal, 2 = Conservative, 3 = NDP, 4 = BQ, 5 = Greens, 6 = People's Party, 7 = other

gen district_outcome = .

replace district_outcome = 1 if constituencynumber == 10001
replace district_outcome = 1 if constituencynumber == 10002
replace district_outcome = 1 if constituencynumber == 10003
replace district_outcome = 1 if constituencynumber == 10004
replace district_outcome = 1 if constituencynumber == 10005
replace district_outcome = 3 if constituencynumber == 10006
replace district_outcome = 1 if constituencynumber == 10007
replace district_outcome = 1 if constituencynumber == 11001
replace district_outcome = 1 if constituencynumber == 11002
replace district_outcome = 1 if constituencynumber == 11003
replace district_outcome = 1 if constituencynumber == 11004
replace district_outcome = 1 if constituencynumber == 12001
replace district_outcome = 1 if constituencynumber == 12002
replace district_outcome = 1 if constituencynumber == 12003
replace district_outcome = 1 if constituencynumber == 12004
replace district_outcome = 1 if constituencynumber == 12005
replace district_outcome = 1 if constituencynumber == 12006
replace district_outcome = 1 if constituencynumber == 12007
replace district_outcome = 1 if constituencynumber == 12008
replace district_outcome = 1 if constituencynumber == 12009
replace district_outcome = 1 if constituencynumber == 12010
replace district_outcome = 2 if constituencynumber == 12011
replace district_outcome = 1 if constituencynumber == 13001
replace district_outcome = 1 if constituencynumber == 13002
replace district_outcome = 5 if constituencynumber == 13003
replace district_outcome = 2 if constituencynumber == 13004
replace district_outcome = 1 if constituencynumber == 13005
replace district_outcome = 1 if constituencynumber == 13006
replace district_outcome = 1 if constituencynumber == 13007
replace district_outcome = 2 if constituencynumber == 13008
replace district_outcome = 1 if constituencynumber == 13009
replace district_outcome = 2 if constituencynumber == 13010
replace district_outcome = 4 if constituencynumber == 24001
replace district_outcome = 4 if constituencynumber == 24002
replace district_outcome = 1 if constituencynumber == 24003
replace district_outcome = 1 if constituencynumber == 24004
replace district_outcome = 1 if constituencynumber == 24005
replace district_outcome = 4 if constituencynumber == 24006
replace district_outcome = 2 if constituencynumber == 24007
replace district_outcome = 4 if constituencynumber == 24008
replace district_outcome = 4 if constituencynumber == 24009
replace district_outcome = 2 if constituencynumber == 24010
replace district_outcome = 4 if constituencynumber == 24011
replace district_outcome = 4 if constituencynumber == 24012
replace district_outcome = 4 if constituencynumber == 24013
replace district_outcome = 4 if constituencynumber == 24014
replace district_outcome = 1 if constituencynumber == 24015
replace district_outcome = 1 if constituencynumber == 24016
replace district_outcome = 1 if constituencynumber == 24017
replace district_outcome = 4 if constituencynumber == 24018
replace district_outcome = 2 if constituencynumber == 24019
replace district_outcome = 4 if constituencynumber == 24020
replace district_outcome = 1 if constituencynumber == 24021
replace district_outcome = 2 if constituencynumber == 24022
replace district_outcome = 1 if constituencynumber == 24023
replace district_outcome = 1 if constituencynumber == 24024
replace district_outcome = 4 if constituencynumber == 24025
replace district_outcome = 1 if constituencynumber == 24026
replace district_outcome = 1 if constituencynumber == 24027
replace district_outcome = 1 if constituencynumber == 24028
replace district_outcome = 1 if constituencynumber == 24029
replace district_outcome = 1 if constituencynumber == 24030
replace district_outcome = 4 if constituencynumber == 24031
replace district_outcome = 4 if constituencynumber == 24032
replace district_outcome = 4 if constituencynumber == 24033
replace district_outcome = 4 if constituencynumber == 24034
replace district_outcome = 4 if constituencynumber == 24035
replace district_outcome = 1 if constituencynumber == 24036
replace district_outcome = 1 if constituencynumber == 24037
replace district_outcome = 4 if constituencynumber == 24038
replace district_outcome = 1 if constituencynumber == 24039
replace district_outcome = 1 if constituencynumber == 24040
replace district_outcome = 1 if constituencynumber == 24041
replace district_outcome = 2 if constituencynumber == 24042
replace district_outcome = 4 if constituencynumber == 24043
replace district_outcome = 1 if constituencynumber == 24044
replace district_outcome = 2 if constituencynumber == 24045
replace district_outcome = 4 if constituencynumber == 24046
replace district_outcome = 2 if constituencynumber == 24047
replace district_outcome = 4 if constituencynumber == 24048
replace district_outcome = 4 if constituencynumber == 24049
replace district_outcome = 4 if constituencynumber == 24050
replace district_outcome = 2 if constituencynumber == 24051
replace district_outcome = 1 if constituencynumber == 24052
replace district_outcome = 1 if constituencynumber == 24053
replace district_outcome = 1 if constituencynumber == 24054
replace district_outcome = 1 if constituencynumber == 24055
replace district_outcome = 1 if constituencynumber == 24056
replace district_outcome = 1 if constituencynumber == 24057
replace district_outcome = 2 if constituencynumber == 24058
replace district_outcome = 1 if constituencynumber == 24059
replace district_outcome = 4 if constituencynumber == 24060
replace district_outcome = 2 if constituencynumber == 24061
replace district_outcome = 4 if constituencynumber == 24062
replace district_outcome = 4 if constituencynumber == 24063
replace district_outcome = 3 if constituencynumber == 24064
replace district_outcome = 1 if constituencynumber == 24065
replace district_outcome = 4 if constituencynumber == 24066
replace district_outcome = 4 if constituencynumber == 24067
replace district_outcome = 1 if constituencynumber == 24068
replace district_outcome = 1 if constituencynumber == 24069
replace district_outcome = 1 if constituencynumber == 24070
replace district_outcome = 4 if constituencynumber == 24071
replace district_outcome = 4 if constituencynumber == 24072
replace district_outcome = 1 if constituencynumber == 24073
replace district_outcome = 1 if constituencynumber == 24074
replace district_outcome = 4 if constituencynumber == 24075
replace district_outcome = 4 if constituencynumber == 24076
replace district_outcome = 1 if constituencynumber == 24077
replace district_outcome = 1 if constituencynumber == 24078
replace district_outcome = 1 if constituencynumber == 35001
replace district_outcome = 3 if constituencynumber == 35002
replace district_outcome = 2 if constituencynumber == 35003
replace district_outcome = 2 if constituencynumber == 35004
replace district_outcome = 2 if constituencynumber == 35005
replace district_outcome = 1 if constituencynumber == 35006
replace district_outcome = 1 if constituencynumber == 35007
replace district_outcome = 1 if constituencynumber == 35008
replace district_outcome = 1 if constituencynumber == 35009
replace district_outcome = 1 if constituencynumber == 35010
replace district_outcome = 1 if constituencynumber == 35011
replace district_outcome = 1 if constituencynumber == 35012
replace district_outcome = 2 if constituencynumber == 35013
replace district_outcome = 2 if constituencynumber == 35014
replace district_outcome = 1 if constituencynumber == 35015
replace district_outcome = 1 if constituencynumber == 35016
replace district_outcome = 2 if constituencynumber == 35017
replace district_outcome = 1 if constituencynumber == 35018
replace district_outcome = 1 if constituencynumber == 35019
replace district_outcome = 1 if constituencynumber == 35020
replace district_outcome = 1 if constituencynumber == 35021
replace district_outcome = 2 if constituencynumber == 35022
replace district_outcome = 2 if constituencynumber == 35023
replace district_outcome = 1 if constituencynumber == 35024
replace district_outcome = 2 if constituencynumber == 35025
replace district_outcome = 2 if constituencynumber == 35026
replace district_outcome = 1 if constituencynumber == 35027
replace district_outcome = 1 if constituencynumber == 35028
replace district_outcome = 1 if constituencynumber == 35029
replace district_outcome = 2 if constituencynumber == 35030
replace district_outcome = 1 if constituencynumber == 35031
replace district_outcome = 1 if constituencynumber == 35032
replace district_outcome = 2 if constituencynumber == 35033
replace district_outcome = 2 if constituencynumber == 35034
replace district_outcome = 3 if constituencynumber == 35035
replace district_outcome = 1 if constituencynumber == 35036
replace district_outcome = 3 if constituencynumber == 35037
replace district_outcome = 1 if constituencynumber == 35038
replace district_outcome = 2 if constituencynumber == 35039
replace district_outcome = 2 if constituencynumber == 35040
replace district_outcome = 1 if constituencynumber == 35041
replace district_outcome = 2 if constituencynumber == 35042
replace district_outcome = 1 if constituencynumber == 35043
replace district_outcome = 1 if constituencynumber == 35044
replace district_outcome = 1 if constituencynumber == 35045
replace district_outcome = 1 if constituencynumber == 35046
replace district_outcome = 1 if constituencynumber == 35047
replace district_outcome = 2 if constituencynumber == 35048
replace district_outcome = 2 if constituencynumber == 35049
replace district_outcome = 2 if constituencynumber == 35050
replace district_outcome = 3 if constituencynumber == 35051
replace district_outcome = 1 if constituencynumber == 35052
replace district_outcome = 1 if constituencynumber == 35053
replace district_outcome = 1 if constituencynumber == 35054
replace district_outcome = 1 if constituencynumber == 35055
replace district_outcome = 2 if constituencynumber == 35056
replace district_outcome = 1 if constituencynumber == 35057
replace district_outcome = 1 if constituencynumber == 35058
replace district_outcome = 1 if constituencynumber == 35059
replace district_outcome = 1 if constituencynumber == 35060
replace district_outcome = 1 if constituencynumber == 35061
replace district_outcome = 1 if constituencynumber == 35062
replace district_outcome = 1 if constituencynumber == 35063
replace district_outcome = 1 if constituencynumber == 35064
replace district_outcome = 1 if constituencynumber == 35065
replace district_outcome = 1 if constituencynumber == 35066
replace district_outcome = 2 if constituencynumber == 35067
replace district_outcome = 2 if constituencynumber == 35068
replace district_outcome = 1 if constituencynumber == 35069
replace district_outcome = 1 if constituencynumber == 35070
replace district_outcome = 2 if constituencynumber == 35071
replace district_outcome = 1 if constituencynumber == 35072
replace district_outcome = 1 if constituencynumber == 35073
replace district_outcome = 2 if constituencynumber == 35074
replace district_outcome = 1 if constituencynumber == 35075
replace district_outcome = 1 if constituencynumber == 35076
replace district_outcome = 1 if constituencynumber == 35077
replace district_outcome = 1 if constituencynumber == 35078
replace district_outcome = 1 if constituencynumber == 35079
replace district_outcome = 2 if constituencynumber == 35080
replace district_outcome = 1 if constituencynumber == 35081
replace district_outcome = 2 if constituencynumber == 35082
replace district_outcome = 2 if constituencynumber == 35083
replace district_outcome = 1 if constituencynumber == 35084
replace district_outcome = 1 if constituencynumber == 35085
replace district_outcome = 2 if constituencynumber == 35086
replace district_outcome = 1 if constituencynumber == 35087
replace district_outcome = 2 if constituencynumber == 35088
replace district_outcome = 1 if constituencynumber == 35089
replace district_outcome = 1 if constituencynumber == 35090
replace district_outcome = 2 if constituencynumber == 35091
replace district_outcome = 1 if constituencynumber == 35092
replace district_outcome = 1 if constituencynumber == 35093
replace district_outcome = 1 if constituencynumber == 35094
replace district_outcome = 1 if constituencynumber == 35095
replace district_outcome = 1 if constituencynumber == 35096
replace district_outcome = 1 if constituencynumber == 35097
replace district_outcome = 1 if constituencynumber == 35098
replace district_outcome = 2 if constituencynumber == 35099
replace district_outcome = 2 if constituencynumber == 35100
replace district_outcome = 1 if constituencynumber == 35101
replace district_outcome = 2 if constituencynumber == 35102
replace district_outcome = 1 if constituencynumber == 35103
replace district_outcome = 2 if constituencynumber == 35104
replace district_outcome = 1 if constituencynumber == 35105
replace district_outcome = 1 if constituencynumber == 35106
replace district_outcome = 3 if constituencynumber == 35107
replace district_outcome = 1 if constituencynumber == 35108
replace district_outcome = 1 if constituencynumber == 35109
replace district_outcome = 1 if constituencynumber == 35110
replace district_outcome = 1 if constituencynumber == 35111
replace district_outcome = 1 if constituencynumber == 35112
replace district_outcome = 2 if constituencynumber == 35113
replace district_outcome = 1 if constituencynumber == 35114
replace district_outcome = 1 if constituencynumber == 35115
replace district_outcome = 1 if constituencynumber == 35116
replace district_outcome = 3 if constituencynumber == 35117
replace district_outcome = 1 if constituencynumber == 35118
replace district_outcome = 2 if constituencynumber == 35119
replace district_outcome = 1 if constituencynumber == 35120
replace district_outcome = 1 if constituencynumber == 35121
replace district_outcome = 2 if constituencynumber == 46001
replace district_outcome = 2 if constituencynumber == 46002
replace district_outcome = 3 if constituencynumber == 46003
replace district_outcome = 2 if constituencynumber == 46004
replace district_outcome = 3 if constituencynumber == 46005
replace district_outcome = 2 if constituencynumber == 46006
replace district_outcome = 2 if constituencynumber == 46007
replace district_outcome = 2 if constituencynumber == 46008
replace district_outcome = 1 if constituencynumber == 46009
replace district_outcome = 2 if constituencynumber == 46010
replace district_outcome = 3 if constituencynumber == 46011
replace district_outcome = 1 if constituencynumber == 46012
replace district_outcome = 1 if constituencynumber == 46013
replace district_outcome = 1 if constituencynumber == 46014
replace district_outcome = 2 if constituencynumber == 47001
replace district_outcome = 2 if constituencynumber == 47002
replace district_outcome = 2 if constituencynumber == 47003
replace district_outcome = 2 if constituencynumber == 47004
replace district_outcome = 2 if constituencynumber == 47005
replace district_outcome = 2 if constituencynumber == 47006
replace district_outcome = 2 if constituencynumber == 47007
replace district_outcome = 2 if constituencynumber == 47008
replace district_outcome = 2 if constituencynumber == 47009
replace district_outcome = 2 if constituencynumber == 47010
replace district_outcome = 2 if constituencynumber == 47011
replace district_outcome = 2 if constituencynumber == 47012
replace district_outcome = 2 if constituencynumber == 47013
replace district_outcome = 2 if constituencynumber == 47014
replace district_outcome = 2 if constituencynumber == 48001
replace district_outcome = 2 if constituencynumber == 48002
replace district_outcome = 2 if constituencynumber == 48003
replace district_outcome = 2 if constituencynumber == 48004
replace district_outcome = 2 if constituencynumber == 48005
replace district_outcome = 2 if constituencynumber == 48006
replace district_outcome = 2 if constituencynumber == 48007
replace district_outcome = 2 if constituencynumber == 48008
replace district_outcome = 2 if constituencynumber == 48009
replace district_outcome = 2 if constituencynumber == 48010
replace district_outcome = 2 if constituencynumber == 48011
replace district_outcome = 2 if constituencynumber == 48012
replace district_outcome = 2 if constituencynumber == 48013
replace district_outcome = 2 if constituencynumber == 48014
replace district_outcome = 2 if constituencynumber == 48015
replace district_outcome = 2 if constituencynumber == 48016
replace district_outcome = 2 if constituencynumber == 48017
replace district_outcome = 2 if constituencynumber == 48018
replace district_outcome = 3 if constituencynumber == 48019
replace district_outcome = 2 if constituencynumber == 48020
replace district_outcome = 2 if constituencynumber == 48021
replace district_outcome = 2 if constituencynumber == 48022
replace district_outcome = 2 if constituencynumber == 48023
replace district_outcome = 2 if constituencynumber == 48024
replace district_outcome = 2 if constituencynumber == 48025
replace district_outcome = 2 if constituencynumber == 48026
replace district_outcome = 2 if constituencynumber == 48027
replace district_outcome = 2 if constituencynumber == 48028
replace district_outcome = 2 if constituencynumber == 48029
replace district_outcome = 2 if constituencynumber == 48030
replace district_outcome = 2 if constituencynumber == 48031
replace district_outcome = 2 if constituencynumber == 48032
replace district_outcome = 2 if constituencynumber == 48033
replace district_outcome = 2 if constituencynumber == 48034
replace district_outcome = 2 if constituencynumber == 59001
replace district_outcome = 1 if constituencynumber == 59002
replace district_outcome = 3 if constituencynumber == 59003
replace district_outcome = 2 if constituencynumber == 59004
replace district_outcome = 2 if constituencynumber == 59005
replace district_outcome = 2 if constituencynumber == 59006
replace district_outcome = 2 if constituencynumber == 59007
replace district_outcome = 1 if constituencynumber == 59008
replace district_outcome = 3 if constituencynumber == 59009
replace district_outcome = 3 if constituencynumber == 59010
replace district_outcome = 1 if constituencynumber == 59011
replace district_outcome = 1 if constituencynumber == 59012
replace district_outcome = 2 if constituencynumber == 59013
replace district_outcome = 2 if constituencynumber == 59014
replace district_outcome = 2 if constituencynumber == 59015
replace district_outcome = 2 if constituencynumber == 59016
replace district_outcome = 2 if constituencynumber == 59017
replace district_outcome = 5 if constituencynumber == 59018
replace district_outcome = 3 if constituencynumber == 59019
replace district_outcome = 2 if constituencynumber == 59020
replace district_outcome = 1 if constituencynumber == 59021
replace district_outcome = 2 if constituencynumber == 59022
replace district_outcome = 2 if constituencynumber == 59023
replace district_outcome = 2 if constituencynumber == 59024
replace district_outcome = 2 if constituencynumber == 59025
replace district_outcome = 3 if constituencynumber == 59026
replace district_outcome = 5 if constituencynumber == 59027
replace district_outcome = 3 if constituencynumber == 59028
replace district_outcome = 3 if constituencynumber == 59029
replace district_outcome = 2 if constituencynumber == 59030
replace district_outcome = 2 if constituencynumber == 59031
replace district_outcome = 1 if constituencynumber == 59032
replace district_outcome = 1 if constituencynumber == 59033
replace district_outcome = 1 if constituencynumber == 59034
replace district_outcome = 3 if constituencynumber == 59035
replace district_outcome = 7 if constituencynumber == 59036
replace district_outcome = 3 if constituencynumber == 59037
replace district_outcome = 3 if constituencynumber == 59038
replace district_outcome = 1 if constituencynumber == 59039
replace district_outcome = 1 if constituencynumber == 59040
replace district_outcome = 3 if constituencynumber == 59041
replace district_outcome = 1 if constituencynumber == 59042
replace district_outcome = 1 if constituencynumber == 60001
replace district_outcome = 1 if constituencynumber == 61001
replace district_outcome = 3 if constituencynumber == 62001


*********************
** COMPETITIVENESS **
*********************

* Margin of victory by district

gen margin = .

replace margin = 15.2 if constituencynumber == 10001
replace margin = 6.200001 if constituencynumber == 10002
replace margin = 13 if constituencynumber == 10003
replace margin = 11.4 if constituencynumber == 10004
replace margin = 19.1 if constituencynumber == 10005
replace margin = 13.7 if constituencynumber == 10006
replace margin = 24.3 if constituencynumber == 10007
replace margin = 20.3 if constituencynumber == 11001
replace margin = 21 if constituencynumber == 11002
replace margin = 5.299999 if constituencynumber == 11003
replace margin = 14.9 if constituencynumber == 11004
replace margin = 4.400002 if constituencynumber == 12001
replace margin = 16.9 if constituencynumber == 12002
replace margin = 1 if constituencynumber == 12003
replace margin = 18.3 if constituencynumber == 12004
replace margin = 12.5 if constituencynumber == 12005
replace margin = 30.2 if constituencynumber == 12006
replace margin = 18.5 if constituencynumber == 12007
replace margin = 16.3 if constituencynumber == 12008
replace margin = 13.6 if constituencynumber == 12009
replace margin = 3.199999 if constituencynumber == 12010
replace margin = 2.899998 if constituencynumber == 12011
replace margin = 33.6 if constituencynumber == 13001
replace margin = 19.9 if constituencynumber == 13002
replace margin = 3.300001 if constituencynumber == 13003
replace margin = 20.4 if constituencynumber == 13004
replace margin = 21.9 if constituencynumber == 13005
replace margin = 1.099998 if constituencynumber == 13006
replace margin = 19.5 if constituencynumber == 13007
replace margin = 23.6 if constituencynumber == 13008
replace margin = 3.400002 if constituencynumber == 13009
replace margin = 25.1 if constituencynumber == 13010
replace margin = 7.799999 if constituencynumber == 24001
replace margin = 20.7 if constituencynumber == 24002
replace margin = 30.7 if constituencynumber == 24003
replace margin = 19.3 if constituencynumber == 24004
replace margin = 1.5 if constituencynumber == 24005
replace margin = 17.5 if constituencynumber == 24006
replace margin = 10.2 if constituencynumber == 24007
replace margin = 3.900002 if constituencynumber == 24008
replace margin = 38.9 if constituencynumber == 24009
replace margin = 27.2 if constituencynumber == 24010
replace margin = 27.4 if constituencynumber == 24011
replace margin = 2.599998 if constituencynumber == 24012
replace margin = 6 if constituencynumber == 24013
replace margin = 22.5 if constituencynumber == 24014
replace margin = 35.2 if constituencynumber == 24015
replace margin = 3.799999 if constituencynumber == 24016
replace margin = 34.3 if constituencynumber == 24017
replace margin = 9.299999 if constituencynumber == 24018
replace margin = 10.8 if constituencynumber == 24019
replace margin = 6.700001 if constituencynumber == 24020
replace margin = 1.200001 if constituencynumber == 24021
replace margin = 1.899998 if constituencynumber == 24022
replace margin = 5.4 if constituencynumber == 24023
replace margin = 35.8 if constituencynumber == 24024
replace margin = 27.4 if constituencynumber == 24025
replace margin = 1.700001 if constituencynumber == 24026
replace margin = 30.7 if constituencynumber == 24027
replace margin = .7000008 if constituencynumber == 24028
replace margin = 38.9 if constituencynumber == 24029
replace margin = 39.5 if constituencynumber == 24030
replace margin = 35.7 if constituencynumber == 24031
replace margin = 11 if constituencynumber == 24032
replace margin = 16.4 if constituencynumber == 24033
replace margin = 5.200001 if constituencynumber == 24034
replace margin = 18.9 if constituencynumber == 24035
replace margin = 42.9 if constituencynumber == 24036
replace margin = 19.4 if constituencynumber == 24037
replace margin = 13.7 if constituencynumber == 24038
replace margin = 16.6 if constituencynumber == 24039
replace margin = 27.6 if constituencynumber == 24040
replace margin = 2.5 if constituencynumber == 24041
replace margin = 19.5 if constituencynumber == 24042
replace margin = 4.299999 if constituencynumber == 24043
replace margin = 12.5 if constituencynumber == 24044
replace margin = 22.3 if constituencynumber == 24045
replace margin = 34.6 if constituencynumber == 24046
replace margin = 23.4 if constituencynumber == 24047
replace margin = 26.2 if constituencynumber == 24048
replace margin = 7.200001 if constituencynumber == 24049
replace margin = 37.6 if constituencynumber == 24050
replace margin = 9.400002 if constituencynumber == 24051
replace margin = 31.4 if constituencynumber == 24052
replace margin = 40.9 if constituencynumber == 24053
replace margin = 26.1 if constituencynumber == 24054
replace margin = 31.9 if constituencynumber == 24055
replace margin = 38.7 if constituencynumber == 24056
replace margin = 32.1 if constituencynumber == 24057
replace margin = 19.2 if constituencynumber == 24058
replace margin = .5999985 if constituencynumber == 24059
replace margin = 25.5 if constituencynumber == 24060
replace margin = 17.1 if constituencynumber == 24061
replace margin = 4.5 if constituencynumber == 24062
replace margin = 29.7 if constituencynumber == 24063
replace margin = 18.3 if constituencynumber == 24064
replace margin = 12.1 if constituencynumber == 24065
replace margin = 20.1 if constituencynumber == 24066
replace margin = 14.2 if constituencynumber == 24067
replace margin = 41.2 if constituencynumber == 24068
replace margin = 49.4 if constituencynumber == 24069
replace margin = 5.399998 if constituencynumber == 24070
replace margin = 18 if constituencynumber == 24071
replace margin = 1.5 if constituencynumber == 24072
replace margin = 1 if constituencynumber == 24073
replace margin = 22.9 if constituencynumber == 24074
replace margin = 21.3 if constituencynumber == 24075
replace margin = 2.4 if constituencynumber == 24076
replace margin = 37.7 if constituencynumber == 24077
replace margin = 19.9 if constituencynumber == 24078
replace margin = 31.7 if constituencynumber == 35001
replace margin = 15.4 if constituencynumber == 35002
replace margin = 2 if constituencynumber == 35003
replace margin = 14.5 if constituencynumber == 35004
replace margin = 7.799999 if constituencynumber == 35005
replace margin = 2.400002 if constituencynumber == 35006
replace margin = 35.8 if constituencynumber == 35007
replace margin = 20.3 if constituencynumber == 35008
replace margin = 21.1 if constituencynumber == 35009
replace margin = 23.7 if constituencynumber == 35010
replace margin = 21.1 if constituencynumber == 35011
replace margin = 29.6 if constituencynumber == 35012
replace margin = 9.599998 if constituencynumber == 35013
replace margin = 16 if constituencynumber == 35014
replace margin = 15.4 if constituencynumber == 35015
replace margin = 9.5 if constituencynumber == 35016
replace margin = 15.7 if constituencynumber == 35017
replace margin = 2.700001 if constituencynumber == 35018
replace margin = 35.9 if constituencynumber == 35019
replace margin = 15 if constituencynumber == 35020
replace margin = 24.6 if constituencynumber == 35021
replace margin = 9 if constituencynumber == 35022
replace margin = 9.899998 if constituencynumber == 35023
replace margin = 20.2 if constituencynumber == 35024
replace margin = 27 if constituencynumber == 35025
replace margin = 6.800003 if constituencynumber == 35026
replace margin = 17.4 if constituencynumber == 35027
replace margin = 23.2 if constituencynumber == 35028
replace margin = 39.2 if constituencynumber == 35029
replace margin = 2.600002 if constituencynumber == 35030
replace margin = 11.6 if constituencynumber == 35031
replace margin = 14.9 if constituencynumber == 35032
replace margin = 22.3 if constituencynumber == 35033
replace margin = 23.1 if constituencynumber == 35034
replace margin = 17.5 if constituencynumber == 35035
replace margin = 9.999998 if constituencynumber == 35036
replace margin = 5.799999 if constituencynumber == 35037
replace margin = 19.9 if constituencynumber == 35038
replace margin = 4.300003 if constituencynumber == 35039
replace margin = 15.4 if constituencynumber == 35040
replace margin = 6.599998 if constituencynumber == 35041
replace margin = 4 if constituencynumber == 35042
replace margin = 1.799999 if constituencynumber == 35043
replace margin = 22.5 if constituencynumber == 35044
replace margin = 10.7 if constituencynumber == 35045
replace margin = .7000008 if constituencynumber == 35046
replace margin = 6.700001 if constituencynumber == 35047
replace margin = 23.6 if constituencynumber == 35048
replace margin = 23.4 if constituencynumber == 35049
replace margin = 22.5 if constituencynumber == 35050
replace margin = 13.9 if constituencynumber == 35051
replace margin = 19.1 if constituencynumber == 35052
replace margin = 15.1 if constituencynumber == 35053
replace margin = 8.300001 if constituencynumber == 35054
replace margin = 19.3 if constituencynumber == 35055
replace margin = 10.5 if constituencynumber == 35056
replace margin = 15.6 if constituencynumber == 35057
replace margin = 26.3 if constituencynumber == 35058
replace margin = 19.5 if constituencynumber == 35059
replace margin = 20.8 if constituencynumber == 35060
replace margin = 11.1 if constituencynumber == 35061
replace margin = 31.7 if constituencynumber == 35062
replace margin = 17.3 if constituencynumber == 35063
replace margin = 12.4 if constituencynumber == 35064
replace margin = 5.299999 if constituencynumber == 35065
replace margin = 4 if constituencynumber == 35066
replace margin = 3 if constituencynumber == 35067
replace margin = 13 if constituencynumber == 35068
replace margin = 6.900002 if constituencynumber == 35069
replace margin = 13.3 if constituencynumber == 35070
replace margin = 3.5 if constituencynumber == 35071
replace margin = 7.200001 if constituencynumber == 35072
replace margin = 10.3 if constituencynumber == 35073
replace margin = 10.4 if constituencynumber == 35074
replace margin = 19.7 if constituencynumber == 35075
replace margin = 26.1 if constituencynumber == 35076
replace margin = 27.8 if constituencynumber == 35077
replace margin = 30 if constituencynumber == 35078
replace margin = 18.5 if constituencynumber == 35079
replace margin = 27.9 if constituencynumber == 35080
replace margin = 15.9 if constituencynumber == 35081
replace margin = 11.4 if constituencynumber == 35082
replace margin = 19.2 if constituencynumber == 35083
replace margin = 4.399998 if constituencynumber == 35084
replace margin = 21.9 if constituencynumber == 35085
replace margin = 33.1 if constituencynumber == 35086
replace margin = .4000015 if constituencynumber == 35087
replace margin = 8.099998 if constituencynumber == 35088
replace margin = 8.6 if constituencynumber == 35089
replace margin = 32.7 if constituencynumber == 35090
replace margin = 27.6 if constituencynumber == 35091
replace margin = 6.899998 if constituencynumber == 35092
replace margin = 13.5 if constituencynumber == 35093
replace margin = 32.9 if constituencynumber == 35094
replace margin = 38.7 if constituencynumber == 35095
replace margin = 23.3 if constituencynumber == 35096
replace margin = 42.1 if constituencynumber == 35097
replace margin = 36.5 if constituencynumber == 35098
replace margin = 11.8 if constituencynumber == 35099
replace margin = 12.6 if constituencynumber == 35100
replace margin = 35.7 if constituencynumber == 35101
replace margin = 28.3 if constituencynumber == 35102
replace margin = 12 if constituencynumber == 35103
replace margin = 19.2 if constituencynumber == 35104
replace margin = 6 if constituencynumber == 35105
replace margin = 17.3 if constituencynumber == 35106
replace margin = 13.5 if constituencynumber == 35107
replace margin = 35.1 if constituencynumber == 35108
replace margin = 14.5 if constituencynumber == 35109
replace margin = 29.8 if constituencynumber == 35110
replace margin = 15 if constituencynumber == 35111
replace margin = 24.3 if constituencynumber == 35112
replace margin = 19 if constituencynumber == 35113
replace margin = 8.200001 if constituencynumber == 35114
replace margin = 12.8 if constituencynumber == 35115
replace margin = 1.100002 if constituencynumber == 35116
replace margin = 3.700001 if constituencynumber == 35117
replace margin = 13.5 if constituencynumber == 35118
replace margin = 19.5 if constituencynumber == 35119
replace margin = 39.5 if constituencynumber == 35120
replace margin = 42.1 if constituencynumber == 35121
replace margin = 49.4 if constituencynumber == 46001
replace margin = 5.200001 if constituencynumber == 46002
replace margin = 26.6 if constituencynumber == 46003
replace margin = 50.2 if constituencynumber == 46004
replace margin = 8.099998 if constituencynumber == 46005
replace margin = 16.9 if constituencynumber == 46006
replace margin = 60.1 if constituencynumber == 46007
replace margin = 52.8 if constituencynumber == 46008
replace margin = 10.3 if constituencynumber == 46009
replace margin = 44.8 if constituencynumber == 46010
replace margin = 7.5 if constituencynumber == 46011
replace margin = 21.7 if constituencynumber == 46012
replace margin = 3.399998 if constituencynumber == 46013
replace margin = 15.3 if constituencynumber == 46014
replace margin = 66.9 if constituencynumber == 47001
replace margin = 71.6 if constituencynumber == 47002
replace margin = 13.9 if constituencynumber == 47003
replace margin = 66.3 if constituencynumber == 47004
replace margin = 54.1 if constituencynumber == 47005
replace margin = 50.3 if constituencynumber == 47006
replace margin = 23.9 if constituencynumber == 47007
replace margin = 43.3 if constituencynumber == 47008
replace margin = 15.8 if constituencynumber == 47009
replace margin = 27.7 if constituencynumber == 47010
replace margin = 22.3 if constituencynumber == 47011
replace margin = 7.400002 if constituencynumber == 47012
replace margin = 76.7 if constituencynumber == 47013
replace margin = 64 if constituencynumber == 47014
replace margin = 60.3 if constituencynumber == 48001
replace margin = 80.4 if constituencynumber == 48002
replace margin = 78.1 if constituencynumber == 48003
replace margin = 29.6 if constituencynumber == 48004
replace margin = 32.5 if constituencynumber == 48005
replace margin = 37.9 if constituencynumber == 48006
replace margin = 56.7 if constituencynumber == 48007
replace margin = 63.3 if constituencynumber == 48008
replace margin = 54.1 if constituencynumber == 48009
replace margin = 49.9 if constituencynumber == 48010
replace margin = 63.9 if constituencynumber == 48011
replace margin = 54.7 if constituencynumber == 48012
replace margin = 24.2 if constituencynumber == 48013
replace margin = 8.400002 if constituencynumber == 48014
replace margin = 26.3 if constituencynumber == 48015
replace margin = 34.4 if constituencynumber == 48016
replace margin = 16.7 if constituencynumber == 48017
replace margin = 34.4 if constituencynumber == 48018
replace margin = 10.2 if constituencynumber == 48019
replace margin = 40.8 if constituencynumber == 48020
replace margin = 60 if constituencynumber == 48021
replace margin = 76.2 if constituencynumber == 48022
replace margin = 70.4 if constituencynumber == 48023
replace margin = 77 if constituencynumber == 48024
replace margin = 77.4 if constituencynumber == 48025
replace margin = 51.1 if constituencynumber == 48026
replace margin = 70.5 if constituencynumber == 48027
replace margin = 73.2 if constituencynumber == 48028
replace margin = 73 if constituencynumber == 48029
replace margin = 70.9 if constituencynumber == 48030
replace margin = 41.5 if constituencynumber == 48031
replace margin = 61.3 if constituencynumber == 48032
replace margin = 67.4 if constituencynumber == 48033
replace margin = 75.1 if constituencynumber == 48034
replace margin = 29.8 if constituencynumber == 59001
replace margin = 3.200001 if constituencynumber == 59002
replace margin = 6.800001 if constituencynumber == 59003
replace margin = 32.7 if constituencynumber == 59004
replace margin = 22.9 if constituencynumber == 59005
replace margin = 29.4 if constituencynumber == 59006
replace margin = 2.5 if constituencynumber == 59007
replace margin = .7000008 if constituencynumber == 59008
replace margin = 8.100002 if constituencynumber == 59009
replace margin = 10.1 if constituencynumber == 59010
replace margin = 8.200001 if constituencynumber == 59011
replace margin = 3.799999 if constituencynumber == 59012
replace margin = 17.5 if constituencynumber == 59013
replace margin = 12.9 if constituencynumber == 59014
replace margin = 10.4 if constituencynumber == 59015
replace margin = 21.4 if constituencynumber == 59016
replace margin = 15.7 if constituencynumber == 59017
replace margin = 8.699999 if constituencynumber == 59018
replace margin = 20.8 if constituencynumber == 59019
replace margin = 26.2 if constituencynumber == 59020
replace margin = 16 if constituencynumber == 59021
replace margin = 6.5 if constituencynumber == 59022
replace margin = .3000011 if constituencynumber == 59023
replace margin = 58.2 if constituencynumber == 59024
replace margin = 20.5 if constituencynumber == 59025
replace margin = 7.699999 if constituencynumber == 59026
replace margin = 28.9 if constituencynumber == 59027
replace margin = 7.700001 if constituencynumber == 59028
replace margin = 1.200001 if constituencynumber == 59029
replace margin = 4.5 if constituencynumber == 59030
replace margin = 6.600002 if constituencynumber == 59031
replace margin = 9.900002 if constituencynumber == 59032
replace margin = 15.8 if constituencynumber == 59033
replace margin = 18.5 if constituencynumber == 59034
replace margin = 34.5 if constituencynumber == 59035
replace margin = 5.999998 if constituencynumber == 59036
replace margin = 5.300003 if constituencynumber == 59037
replace margin = 26 if constituencynumber == 59038
replace margin = 15.8 if constituencynumber == 59039
replace margin = 7.900002 if constituencynumber == 59040
replace margin = 3.300001 if constituencynumber == 59041
replace margin = 8.200001 if constituencynumber == 59042
replace margin = .7999992 if constituencynumber == 60001
replace margin = 14.2 if constituencynumber == 61001
replace margin = 9.9 if constituencynumber == 62001

* ENEP by district

gen enep = .

replace enep = 2.907526 if constituencynumber == 10001
replace enep = 2.630783 if constituencynumber == 10002
replace enep = 2.667563 if constituencynumber == 10003
replace enep = 2.960586 if constituencynumber == 10004
replace enep = 2.895906 if constituencynumber == 10005
replace enep = 2.752773 if constituencynumber == 10006
replace enep = 2.703996 if constituencynumber == 10007
replace enep = 2.850603 if constituencynumber == 11001
replace enep = 3.285454 if constituencynumber == 11002
replace enep = 3.136074 if constituencynumber == 11003
replace enep = 3.211489 if constituencynumber == 11004
replace enep = 3.345287 if constituencynumber == 12001
replace enep = 3.038812 if constituencynumber == 12002
replace enep = 3.399406 if constituencynumber == 12003
replace enep = 3.183497 if constituencynumber == 12004
replace enep = 3.279183 if constituencynumber == 12005
replace enep = 2.995994 if constituencynumber == 12006
replace enep = 3.393028 if constituencynumber == 12007
replace enep = 3.527175 if constituencynumber == 12008
replace enep = 3.426746 if constituencynumber == 12009
replace enep = 4.247799 if constituencynumber == 12010
replace enep = 3.178599 if constituencynumber == 12011
replace enep = 2.640111 if constituencynumber == 13001
replace enep = 3.089414 if constituencynumber == 13002
replace enep = 3.509191 if constituencynumber == 13003
replace enep = 3.22409 if constituencynumber == 13004
replace enep = 2.778071 if constituencynumber == 13005
replace enep = 3.509215 if constituencynumber == 13006
replace enep = 3.4846 if constituencynumber == 13007
replace enep = 3.011676 if constituencynumber == 13008
replace enep = 3.54134 if constituencynumber == 13009
replace enep = 2.913244 if constituencynumber == 13010
replace enep = 3.899396 if constituencynumber == 24001
replace enep = 3.302194 if constituencynumber == 24002
replace enep = 2.908397 if constituencynumber == 24003
replace enep = 3.027697 if constituencynumber == 24004
replace enep = 3.36226 if constituencynumber == 24005
replace enep = 2.583525 if constituencynumber == 24006
replace enep = 3.786775 if constituencynumber == 24007
replace enep = 4.1287 if constituencynumber == 24008
replace enep = 2.611627 if constituencynumber == 24009
replace enep = 2.982929 if constituencynumber == 24010
replace enep = 2.980431 if constituencynumber == 24011
replace enep = 3.402066 if constituencynumber == 24012
replace enep = 3.135425 if constituencynumber == 24013
replace enep = 2.816235 if constituencynumber == 24014
replace enep = 2.534366 if constituencynumber == 24015
replace enep = 3.45681 if constituencynumber == 24016
replace enep = 2.837596 if constituencynumber == 24017
replace enep = 3.555733 if constituencynumber == 24018
replace enep = 3.68411 if constituencynumber == 24019
replace enep = 3.712393 if constituencynumber == 24020
replace enep = 3.271181 if constituencynumber == 24021
replace enep = 3.426676 if constituencynumber == 24022
replace enep = 3.650288 if constituencynumber == 24023
replace enep = 2.965344 if constituencynumber == 24024
replace enep = 3.507258 if constituencynumber == 24025
replace enep = 2.806608 if constituencynumber == 24026
replace enep = 2.926304 if constituencynumber == 24027
replace enep = 3.601475 if constituencynumber == 24028
replace enep = 2.497921 if constituencynumber == 24029
replace enep = 2.891527 if constituencynumber == 24030
replace enep = 2.491901 if constituencynumber == 24031
replace enep = 3.895674 if constituencynumber == 24032
replace enep = 3.032785 if constituencynumber == 24033
replace enep = 3.081541 if constituencynumber == 24034
replace enep = 3.19501 if constituencynumber == 24035
replace enep = 2.597686 if constituencynumber == 24036
replace enep = 3.51881 if constituencynumber == 24037
replace enep = 2.935177 if constituencynumber == 24038
replace enep = 3.393811 if constituencynumber == 24039
replace enep = 3.211768 if constituencynumber == 24040
replace enep = 3.279818 if constituencynumber == 24041
replace enep = 3.362882 if constituencynumber == 24042
replace enep = 3.457432 if constituencynumber == 24043
replace enep = 3.552221 if constituencynumber == 24044
replace enep = 3.346104 if constituencynumber == 24045
replace enep = 2.724231 if constituencynumber == 24046
replace enep = 2.979205 if constituencynumber == 24047
replace enep = 2.933894 if constituencynumber == 24048
replace enep = 3.082082 if constituencynumber == 24049
replace enep = 2.548992 if constituencynumber == 24050
replace enep = 3.225682 if constituencynumber == 24051
replace enep = 2.560479 if constituencynumber == 24052
replace enep = 2.721348 if constituencynumber == 24053
replace enep = 3.426394 if constituencynumber == 24054
replace enep = 3.021796 if constituencynumber == 24055
replace enep = 2.706118 if constituencynumber == 24056
replace enep = 3.244741 if constituencynumber == 24057
replace enep = 3.40791 if constituencynumber == 24058
replace enep = 3.896463 if constituencynumber == 24059
replace enep = 2.693124 if constituencynumber == 24060
replace enep = 3.196032 if constituencynumber == 24061
replace enep = 3.204687 if constituencynumber == 24062
replace enep = 2.922626 if constituencynumber == 24063
replace enep = 3.334378 if constituencynumber == 24064
replace enep = 3.11379 if constituencynumber == 24065
replace enep = 3.666576 if constituencynumber == 24066
replace enep = 3.180864 if constituencynumber == 24067
replace enep = 2.551164 if constituencynumber == 24068
replace enep = 2.447016 if constituencynumber == 24069
replace enep = 3.285421 if constituencynumber == 24070
replace enep = 3.005349 if constituencynumber == 24071
replace enep = 3.250003 if constituencynumber == 24072
replace enep = 4.055529 if constituencynumber == 24073
replace enep = 3.217979 if constituencynumber == 24074
replace enep = 2.819054 if constituencynumber == 24075
replace enep = 4.141661 if constituencynumber == 24076
replace enep = 2.930377 if constituencynumber == 24077
replace enep = 3.074359 if constituencynumber == 24078
replace enep = 2.409894 if constituencynumber == 35001
replace enep = 3.287765 if constituencynumber == 35002
replace enep = 2.604322 if constituencynumber == 35003
replace enep = 3.199805 if constituencynumber == 35004
replace enep = 3.462208 if constituencynumber == 35005
replace enep = 3.136674 if constituencynumber == 35006
replace enep = 2.521642 if constituencynumber == 35007
replace enep = 2.976049 if constituencynumber == 35008
replace enep = 2.847543 if constituencynumber == 35009
replace enep = 2.699157 if constituencynumber == 35010
replace enep = 2.82269 if constituencynumber == 35011
replace enep = 2.646203 if constituencynumber == 35012
replace enep = 3.333256 if constituencynumber == 35013
replace enep = 3.073377 if constituencynumber == 35014
replace enep = 2.767408 if constituencynumber == 35015
replace enep = 3.449073 if constituencynumber == 35016
replace enep = 2.919486 if constituencynumber == 35017
replace enep = 2.703887 if constituencynumber == 35018
replace enep = 2.33321 if constituencynumber == 35019
replace enep = 2.567941 if constituencynumber == 35020
replace enep = 2.403893 if constituencynumber == 35021
replace enep = 3.220602 if constituencynumber == 35022
replace enep = 3.150946 if constituencynumber == 35023
replace enep = 2.48351 if constituencynumber == 35024
replace enep = 2.930686 if constituencynumber == 35025
replace enep = 3.043584 if constituencynumber == 35026
replace enep = 2.522723 if constituencynumber == 35027
replace enep = 2.705159 if constituencynumber == 35028
replace enep = 2.276053 if constituencynumber == 35029
replace enep = 3.136546 if constituencynumber == 35030
replace enep = 2.714581 if constituencynumber == 35031
replace enep = 3.560328 if constituencynumber == 35032
replace enep = 3.226015 if constituencynumber == 35033
replace enep = 2.961147 if constituencynumber == 35034
replace enep = 3.098556 if constituencynumber == 35035
replace enep = 3.357136 if constituencynumber == 35036
replace enep = 3.443965 if constituencynumber == 35037
replace enep = 3.070706 if constituencynumber == 35038
replace enep = 3.025105 if constituencynumber == 35039
replace enep = 2.762965 if constituencynumber == 35040
replace enep = 2.948435 if constituencynumber == 35041
replace enep = 3.447301 if constituencynumber == 35042
replace enep = 2.530012 if constituencynumber == 35043
replace enep = 3.218798 if constituencynumber == 35044
replace enep = 3.675403 if constituencynumber == 35045
replace enep = 3.036274 if constituencynumber == 35046
replace enep = 3.292018 if constituencynumber == 35047
replace enep = 2.987804 if constituencynumber == 35048
replace enep = 3.075797 if constituencynumber == 35049
replace enep = 2.958947 if constituencynumber == 35050
replace enep = 3.297566 if constituencynumber == 35051
replace enep = 3.34295 if constituencynumber == 35052
replace enep = 3.207616 if constituencynumber == 35053
replace enep = 3.41774 if constituencynumber == 35054
replace enep = 2.401248 if constituencynumber == 35055
replace enep = 2.543144 if constituencynumber == 35056
replace enep = 2.474935 if constituencynumber == 35057
replace enep = 2.445903 if constituencynumber == 35058
replace enep = 2.477369 if constituencynumber == 35059
replace enep = 2.482967 if constituencynumber == 35060
replace enep = 2.612979 if constituencynumber == 35061
replace enep = 2.416364 if constituencynumber == 35062
replace enep = 2.656311 if constituencynumber == 35063
replace enep = 2.905794 if constituencynumber == 35064
replace enep = 2.908778 if constituencynumber == 35065
replace enep = 3.414216 if constituencynumber == 35066
replace enep = 3.678418 if constituencynumber == 35067
replace enep = 3.036883 if constituencynumber == 35068
replace enep = 3.294057 if constituencynumber == 35069
replace enep = 3.48477 if constituencynumber == 35070
replace enep = 3.176469 if constituencynumber == 35071
replace enep = 2.657652 if constituencynumber == 35072
replace enep = 2.586452 if constituencynumber == 35073
replace enep = 3.332589 if constituencynumber == 35074
replace enep = 2.91757 if constituencynumber == 35075
replace enep = 2.562907 if constituencynumber == 35076
replace enep = 2.75923 if constituencynumber == 35077
replace enep = 2.912828 if constituencynumber == 35078
replace enep = 3.119872 if constituencynumber == 35079
replace enep = 3.15861 if constituencynumber == 35080
replace enep = 2.893929 if constituencynumber == 35081
replace enep = 3.283059 if constituencynumber == 35082
replace enep = 3.148109 if constituencynumber == 35083
replace enep = 3.22193 if constituencynumber == 35084
replace enep = 2.755261 if constituencynumber == 35085
replace enep = 2.918149 if constituencynumber == 35086
replace enep = 2.604201 if constituencynumber == 35087
replace enep = 2.692051 if constituencynumber == 35088
replace enep = 3.244372 if constituencynumber == 35089
replace enep = 2.69311 if constituencynumber == 35090
replace enep = 2.963112 if constituencynumber == 35091
replace enep = 3.223031 if constituencynumber == 35092
replace enep = 2.498358 if constituencynumber == 35093
replace enep = 2.684535 if constituencynumber == 35094
replace enep = 2.287654 if constituencynumber == 35095
replace enep = 2.52378 if constituencynumber == 35096
replace enep = 2.25858 if constituencynumber == 35097
replace enep = 2.521254 if constituencynumber == 35098
replace enep = 3.168257 if constituencynumber == 35099
replace enep = 3.202193 if constituencynumber == 35100
replace enep = 2.593563 if constituencynumber == 35101
replace enep = 2.64138 if constituencynumber == 35102
replace enep = 3.354613 if constituencynumber == 35103
replace enep = 2.333319 if constituencynumber == 35104
replace enep = 3.36148 if constituencynumber == 35105
replace enep = 3.317278 if constituencynumber == 35106
replace enep = 3.275617 if constituencynumber == 35107
replace enep = 2.506806 if constituencynumber == 35108
replace enep = 2.831538 if constituencynumber == 35109
replace enep = 2.864164 if constituencynumber == 35110
replace enep = 2.487587 if constituencynumber == 35111
replace enep = 3.021413 if constituencynumber == 35112
replace enep = 3.024858 if constituencynumber == 35113
replace enep = 2.941739 if constituencynumber == 35114
replace enep = 2.622428 if constituencynumber == 35115
replace enep = 3.388613 if constituencynumber == 35116
replace enep = 3.037667 if constituencynumber == 35117
replace enep = 2.516856 if constituencynumber == 35118
replace enep = 3.178791 if constituencynumber == 35119
replace enep = 2.4471 if constituencynumber == 35120
replace enep = 2.292316 if constituencynumber == 35121
replace enep = 2.255793 if constituencynumber == 46001
replace enep = 3.165348 if constituencynumber == 46002
replace enep = 2.847162 if constituencynumber == 46003
replace enep = 2.201445 if constituencynumber == 46004
replace enep = 2.739801 if constituencynumber == 46005
replace enep = 3.074218 if constituencynumber == 46006
replace enep = 1.907931 if constituencynumber == 46007
replace enep = 2.119048 if constituencynumber == 46008
replace enep = 3.100458 if constituencynumber == 46009
replace enep = 2.254852 if constituencynumber == 46010
replace enep = 3.155261 if constituencynumber == 46011
replace enep = 2.959009 if constituencynumber == 46012
replace enep = 2.87141 if constituencynumber == 46013
replace enep = 3.066243 if constituencynumber == 46014
replace enep = 1.583978 if constituencynumber == 47001
replace enep = 1.493226 if constituencynumber == 47002
replace enep = 3.027789 if constituencynumber == 47003
replace enep = 1.572846 if constituencynumber == 47004
replace enep = 1.853286 if constituencynumber == 47005
replace enep = 1.999716 if constituencynumber == 47006
replace enep = 2.654886 if constituencynumber == 47007
replace enep = 2.21078 if constituencynumber == 47008
replace enep = 2.671896 if constituencynumber == 47009
replace enep = 2.635393 if constituencynumber == 47010
replace enep = 2.641743 if constituencynumber == 47011
replace enep = 2.52273 if constituencynumber == 47012
replace enep = 1.387912 if constituencynumber == 47013
replace enep = 1.663938 if constituencynumber == 47014
replace enep = 1.882778 if constituencynumber == 48001
replace enep = 1.357383 if constituencynumber == 48002
replace enep = 1.405904 if constituencynumber == 48003
replace enep = 2.468368 if constituencynumber == 48004
replace enep = 2.667407 if constituencynumber == 48005
replace enep = 2.406693 if constituencynumber == 48006
replace enep = 1.889141 if constituencynumber == 48007
replace enep = 1.740511 if constituencynumber == 48008
replace enep = 1.914777 if constituencynumber == 48009
replace enep = 1.96554 if constituencynumber == 48010
replace enep = 1.712992 if constituencynumber == 48011
replace enep = 1.915643 if constituencynumber == 48012
replace enep = 2.642476 if constituencynumber == 48013
replace enep = 3.089719 if constituencynumber == 48014
replace enep = 2.793327 if constituencynumber == 48015
replace enep = 2.559921 if constituencynumber == 48016
replace enep = 2.623226 if constituencynumber == 48017
replace enep = 2.458077 if constituencynumber == 48018
replace enep = 2.662974 if constituencynumber == 48019
replace enep = 2.306358 if constituencynumber == 48020
replace enep = 1.809022 if constituencynumber == 48021
replace enep = 1.464523 if constituencynumber == 48022
replace enep = 1.533617 if constituencynumber == 48023
replace enep = 1.401068 if constituencynumber == 48024
replace enep = 1.40611 if constituencynumber == 48025
replace enep = 2.107895 if constituencynumber == 48026
replace enep = 1.561575 if constituencynumber == 48027
replace enep = 1.509924 if constituencynumber == 48028
replace enep = 1.525099 if constituencynumber == 48029
replace enep = 1.540118 if constituencynumber == 48030
replace enep = 2.328945 if constituencynumber == 48031
replace enep = 1.771724 if constituencynumber == 48032
replace enep = 1.621568 if constituencynumber == 48033
replace enep = 1.464176 if constituencynumber == 48034
replace enep = 2.893309 if constituencynumber == 59001
replace enep = 3.600606 if constituencynumber == 59002
replace enep = 3.361333 if constituencynumber == 59003
replace enep = 2.855136 if constituencynumber == 59004
replace enep = 3.060921 if constituencynumber == 59005
replace enep = 3.072962 if constituencynumber == 59006
replace enep = 3.26649 if constituencynumber == 59007
replace enep = 3.403467 if constituencynumber == 59008
replace enep = 3.208265 if constituencynumber == 59009
replace enep = 3.788539 if constituencynumber == 59010
replace enep = 3.229933 if constituencynumber == 59011
replace enep = 3.282639 if constituencynumber == 59012
replace enep = 3.252255 if constituencynumber == 59013
replace enep = 2.980981 if constituencynumber == 59014
replace enep = 2.975951 if constituencynumber == 59015
replace enep = 3.114353 if constituencynumber == 59016
replace enep = 3.396139 if constituencynumber == 59017
replace enep = 3.831961 if constituencynumber == 59018
replace enep = 3.289885 if constituencynumber == 59019
replace enep = 3.080772 if constituencynumber == 59020
replace enep = 3.343453 if constituencynumber == 59021
replace enep = 3.533269 if constituencynumber == 59022
replace enep = 3.534618 if constituencynumber == 59023
replace enep = 1.945082 if constituencynumber == 59024
replace enep = 2.887737 if constituencynumber == 59025
replace enep = 3.924986 if constituencynumber == 59026
replace enep = 3.069688 if constituencynumber == 59027
replace enep = 3.346138 if constituencynumber == 59028
replace enep = 3.407457 if constituencynumber == 59029
replace enep = 2.984656 if constituencynumber == 59030
replace enep = 3.076421 if constituencynumber == 59031
replace enep = 3.517708 if constituencynumber == 59032
replace enep = 3.001705 if constituencynumber == 59033
replace enep = 3.480016 if constituencynumber == 59034
replace enep = 2.89553 if constituencynumber == 59035
replace enep = 4.084651 if constituencynumber == 59036
replace enep = 3.480573 if constituencynumber == 59037
replace enep = 2.959307 if constituencynumber == 59038
replace enep = 3.287268 if constituencynumber == 59039
replace enep = 3.138515 if constituencynumber == 59040
replace enep = 3.767656 if constituencynumber == 59041
replace enep = 3.804364 if constituencynumber == 59042
replace enep = 3.587122 if constituencynumber == 60001
replace enep = 3.522082 if constituencynumber == 61001
replace enep = 3.025261 if constituencynumber == 62001


*******************************
** INCUMBENT PARTY REELECTED **
*******************************

gen reelected = .

replace reelected = 1 if constituencynumber == 10001
replace reelected = 1 if constituencynumber == 10002
replace reelected = 1 if constituencynumber == 10003
replace reelected = 1 if constituencynumber == 10004
replace reelected = 1 if constituencynumber == 10005
replace reelected = 0 if constituencynumber == 10006
replace reelected = 1 if constituencynumber == 10007
replace reelected = 1 if constituencynumber == 11001
replace reelected = 1 if constituencynumber == 11002
replace reelected = 1 if constituencynumber == 11003
replace reelected = 1 if constituencynumber == 11004
replace reelected = 1 if constituencynumber == 12001
replace reelected = 1 if constituencynumber == 12002
replace reelected = 1 if constituencynumber == 12003
replace reelected = 1 if constituencynumber == 12004
replace reelected = 1 if constituencynumber == 12005
replace reelected = 1 if constituencynumber == 12006
replace reelected = 1 if constituencynumber == 12007
replace reelected = 1 if constituencynumber == 12008
replace reelected = 1 if constituencynumber == 12009
replace reelected = 1 if constituencynumber == 12010
replace reelected = 0 if constituencynumber == 12011
replace reelected = 1 if constituencynumber == 13001
replace reelected = 1 if constituencynumber == 13002
replace reelected = 0 if constituencynumber == 13003
replace reelected = 0 if constituencynumber == 13004
replace reelected = 1 if constituencynumber == 13005
replace reelected = 1 if constituencynumber == 13006
replace reelected = 1 if constituencynumber == 13007
replace reelected = 0 if constituencynumber == 13008
replace reelected = 1 if constituencynumber == 13009
replace reelected = 0 if constituencynumber == 13010
replace reelected = 0 if constituencynumber == 24001
replace reelected = 0 if constituencynumber == 24002
replace reelected = 1 if constituencynumber == 24003
replace reelected = 1 if constituencynumber == 24004
replace reelected = 1 if constituencynumber == 24005
replace reelected = 0 if constituencynumber == 24006
replace reelected = 1 if constituencynumber == 24007
replace reelected = 0 if constituencynumber == 24008
replace reelected = 1 if constituencynumber == 24009
replace reelected = 1 if constituencynumber == 24010
replace reelected = 0 if constituencynumber == 24011
replace reelected = 0 if constituencynumber == 24012
replace reelected = 0 if constituencynumber == 24013
replace reelected = 1 if constituencynumber == 24014
replace reelected = 1 if constituencynumber == 24015
replace reelected = 1 if constituencynumber == 24016
replace reelected = 1 if constituencynumber == 24017
replace reelected = 0 if constituencynumber == 24018
replace reelected = 1 if constituencynumber == 24019
replace reelected = 0 if constituencynumber == 24020
replace reelected = 1 if constituencynumber == 24021
replace reelected = 0 if constituencynumber == 24022
replace reelected = 1 if constituencynumber == 24023
replace reelected = 1 if constituencynumber == 24024
replace reelected = 0 if constituencynumber == 24025
replace reelected = 1 if constituencynumber == 24026
replace reelected = 1 if constituencynumber == 24027
replace reelected = 0 if constituencynumber == 24028
replace reelected = 1 if constituencynumber == 24029
replace reelected = 1 if constituencynumber == 24030
replace reelected = 1 if constituencynumber == 24031
replace reelected = 0 if constituencynumber == 24032
replace reelected = 1 if constituencynumber == 24033
replace reelected = 0 if constituencynumber == 24034
replace reelected = 0 if constituencynumber == 24035
replace reelected = 1 if constituencynumber == 24036
replace reelected = 1 if constituencynumber == 24037
replace reelected = 0 if constituencynumber == 24038
replace reelected = 0 if constituencynumber == 24039
replace reelected = 1 if constituencynumber == 24040
replace reelected = 1 if constituencynumber == 24041
replace reelected = 1 if constituencynumber == 24042
replace reelected = 0 if constituencynumber == 24043
replace reelected = 1 if constituencynumber == 24044
replace reelected = 1 if constituencynumber == 24045
replace reelected = 1 if constituencynumber == 24046
replace reelected = 1 if constituencynumber == 24047
replace reelected = 1 if constituencynumber == 24048
replace reelected = 0 if constituencynumber == 24049
replace reelected = 1 if constituencynumber == 24050
replace reelected = 1 if constituencynumber == 24051
replace reelected = 1 if constituencynumber == 24052
replace reelected = 1 if constituencynumber == 24053
replace reelected = 0 if constituencynumber == 24054
replace reelected = 1 if constituencynumber == 24055
replace reelected = 1 if constituencynumber == 24056
replace reelected = 1 if constituencynumber == 24057
replace reelected = 1 if constituencynumber == 24058
replace reelected = 1 if constituencynumber == 24059
replace reelected = 1 if constituencynumber == 24060
replace reelected = 1 if constituencynumber == 24061
replace reelected = 0 if constituencynumber == 24062
replace reelected = 1 if constituencynumber == 24063
replace reelected = 1 if constituencynumber == 24064
replace reelected = 1 if constituencynumber == 24065
replace reelected = 0 if constituencynumber == 24066
replace reelected = 0 if constituencynumber == 24067
replace reelected = 1 if constituencynumber == 24068
replace reelected = 1 if constituencynumber == 24069
replace reelected = 1 if constituencynumber == 24070
replace reelected = 0 if constituencynumber == 24071
replace reelected = 0 if constituencynumber == 24072
replace reelected = 0 if constituencynumber == 24073
replace reelected = 1 if constituencynumber == 24074
replace reelected = 1 if constituencynumber == 24075
replace reelected = 0 if constituencynumber == 24076
replace reelected = 1 if constituencynumber == 24077
replace reelected = 1 if constituencynumber == 24078
replace reelected = 1 if constituencynumber == 35001
replace reelected = 1 if constituencynumber == 35002
replace reelected = 0 if constituencynumber == 35003
replace reelected = 1 if constituencynumber == 35004
replace reelected = 1 if constituencynumber == 35005
replace reelected = 1 if constituencynumber == 35006
replace reelected = 1 if constituencynumber == 35007
replace reelected = 1 if constituencynumber == 35008
replace reelected = 1 if constituencynumber == 35009
replace reelected = 1 if constituencynumber == 35010
replace reelected = 1 if constituencynumber == 35011
replace reelected = 1 if constituencynumber == 35012
replace reelected = 1 if constituencynumber == 35013
replace reelected = 1 if constituencynumber == 35014
replace reelected = 1 if constituencynumber == 35015
replace reelected = 1 if constituencynumber == 35016
replace reelected = 1 if constituencynumber == 35017
replace reelected = 1 if constituencynumber == 35018
replace reelected = 1 if constituencynumber == 35019
replace reelected = 1 if constituencynumber == 35020
replace reelected = 1 if constituencynumber == 35021
replace reelected = 1 if constituencynumber == 35022
replace reelected = 1 if constituencynumber == 35023
replace reelected = 1 if constituencynumber == 35024
replace reelected = 1 if constituencynumber == 35025
replace reelected = 0 if constituencynumber == 35026
replace reelected = 1 if constituencynumber == 35027
replace reelected = 1 if constituencynumber == 35028
replace reelected = 1 if constituencynumber == 35029
replace reelected = 1 if constituencynumber == 35030
replace reelected = 1 if constituencynumber == 35031
replace reelected = 1 if constituencynumber == 35032
replace reelected = 1 if constituencynumber == 35033
replace reelected = 1 if constituencynumber == 35034
replace reelected = 1 if constituencynumber == 35035
replace reelected = 1 if constituencynumber == 35036
replace reelected = 1 if constituencynumber == 35037
replace reelected = 1 if constituencynumber == 35038
replace reelected = 0 if constituencynumber == 35039
replace reelected = 1 if constituencynumber == 35040
replace reelected = 1 if constituencynumber == 35041
replace reelected = 0 if constituencynumber == 35042
replace reelected = 1 if constituencynumber == 35043
replace reelected = 1 if constituencynumber == 35044
replace reelected = 1 if constituencynumber == 35045
replace reelected = 0 if constituencynumber == 35046
replace reelected = 1 if constituencynumber == 35047
replace reelected = 1 if constituencynumber == 35048
replace reelected = 1 if constituencynumber == 35049
replace reelected = 1 if constituencynumber == 35050
replace reelected = 1 if constituencynumber == 35051
replace reelected = 1 if constituencynumber == 35052
replace reelected = 1 if constituencynumber == 35053
replace reelected = 1 if constituencynumber == 35054
replace reelected = 1 if constituencynumber == 35055
replace reelected = 1 if constituencynumber == 35056
replace reelected = 0 if constituencynumber == 35057
replace reelected = 1 if constituencynumber == 35058
replace reelected = 1 if constituencynumber == 35059
replace reelected = 1 if constituencynumber == 35060
replace reelected = 1 if constituencynumber == 35061
replace reelected = 1 if constituencynumber == 35062
replace reelected = 1 if constituencynumber == 35063
replace reelected = 1 if constituencynumber == 35064
replace reelected = 1 if constituencynumber == 35065
replace reelected = 1 if constituencynumber == 35066
replace reelected = 1 if constituencynumber == 35067
replace reelected = 1 if constituencynumber == 35068
replace reelected = 1 if constituencynumber == 35069
replace reelected = 1 if constituencynumber == 35070
replace reelected = 0 if constituencynumber == 35071
replace reelected = 1 if constituencynumber == 35072
replace reelected = 1 if constituencynumber == 35073
replace reelected = 1 if constituencynumber == 35074
replace reelected = 1 if constituencynumber == 35075
replace reelected = 1 if constituencynumber == 35076
replace reelected = 1 if constituencynumber == 35077
replace reelected = 1 if constituencynumber == 35078
replace reelected = 1 if constituencynumber == 35079
replace reelected = 1 if constituencynumber == 35080
replace reelected = 1 if constituencynumber == 35081
replace reelected = 1 if constituencynumber == 35082
replace reelected = 1 if constituencynumber == 35083
replace reelected = 1 if constituencynumber == 35084
replace reelected = 1 if constituencynumber == 35085
replace reelected = 1 if constituencynumber == 35086
replace reelected = 1 if constituencynumber == 35087
replace reelected = 1 if constituencynumber == 35088
replace reelected = 1 if constituencynumber == 35089
replace reelected = 1 if constituencynumber == 35090
replace reelected = 1 if constituencynumber == 35091
replace reelected = 1 if constituencynumber == 35092
replace reelected = 1 if constituencynumber == 35093
replace reelected = 1 if constituencynumber == 35094
replace reelected = 1 if constituencynumber == 35095
replace reelected = 1 if constituencynumber == 35096
replace reelected = 1 if constituencynumber == 35097
replace reelected = 1 if constituencynumber == 35098
replace reelected = 1 if constituencynumber == 35099
replace reelected = 1 if constituencynumber == 35100
replace reelected = 1 if constituencynumber == 35101
replace reelected = 1 if constituencynumber == 35102
replace reelected = 1 if constituencynumber == 35103
replace reelected = 1 if constituencynumber == 35104
replace reelected = 1 if constituencynumber == 35105
replace reelected = 1 if constituencynumber == 35106
replace reelected = 1 if constituencynumber == 35107
replace reelected = 1 if constituencynumber == 35108
replace reelected = 1 if constituencynumber == 35109
replace reelected = 1 if constituencynumber == 35110
replace reelected = 1 if constituencynumber == 35111
replace reelected = 1 if constituencynumber == 35112
replace reelected = 1 if constituencynumber == 35113
replace reelected = 1 if constituencynumber == 35114
replace reelected = 1 if constituencynumber == 35115
replace reelected = 0 if constituencynumber == 35116
replace reelected = 1 if constituencynumber == 35117
replace reelected = 1 if constituencynumber == 35118
replace reelected = 1 if constituencynumber == 35119
replace reelected = 1 if constituencynumber == 35120
replace reelected = 1 if constituencynumber == 35121
replace reelected = 1 if constituencynumber == 46001
replace reelected = 0 if constituencynumber == 46002
replace reelected = 1 if constituencynumber == 46003
replace reelected = 1 if constituencynumber == 46004
replace reelected = 1 if constituencynumber == 46005
replace reelected = 0 if constituencynumber == 46006
replace reelected = 1 if constituencynumber == 46007
replace reelected = 1 if constituencynumber == 46008
replace reelected = 1 if constituencynumber == 46009
replace reelected = 1 if constituencynumber == 46010
replace reelected = 0 if constituencynumber == 46011
replace reelected = 1 if constituencynumber == 46012
replace reelected = 1 if constituencynumber == 46013
replace reelected = 1 if constituencynumber == 46014
replace reelected = 1 if constituencynumber == 47001
replace reelected = 1 if constituencynumber == 47002
replace reelected = 0 if constituencynumber == 47003
replace reelected = 1 if constituencynumber == 47004
replace reelected = 1 if constituencynumber == 47005
replace reelected = 1 if constituencynumber == 47006
replace reelected = 0 if constituencynumber == 47007
replace reelected = 1 if constituencynumber == 47008
replace reelected = 0 if constituencynumber == 47009
replace reelected = 1 if constituencynumber == 47010
replace reelected = 1 if constituencynumber == 47011
replace reelected = 0 if constituencynumber == 47012
replace reelected = 1 if constituencynumber == 47013
replace reelected = 1 if constituencynumber == 47014
replace reelected = 1 if constituencynumber == 48001
replace reelected = 1 if constituencynumber == 48002
replace reelected = 1 if constituencynumber == 48003
replace reelected = 0 if constituencynumber == 48004
replace reelected = 1 if constituencynumber == 48005
replace reelected = 1 if constituencynumber == 48006
replace reelected = 1 if constituencynumber == 48007
replace reelected = 1 if constituencynumber == 48008
replace reelected = 1 if constituencynumber == 48009
replace reelected = 1 if constituencynumber == 48010
replace reelected = 1 if constituencynumber == 48011
replace reelected = 1 if constituencynumber == 48012
replace reelected = 0 if constituencynumber == 48013
replace reelected = 0 if constituencynumber == 48014
replace reelected = 1 if constituencynumber == 48015
replace reelected = 1 if constituencynumber == 48016
replace reelected = 0 if constituencynumber == 48017
replace reelected = 1 if constituencynumber == 48018
replace reelected = 1 if constituencynumber == 48019
replace reelected = 1 if constituencynumber == 48020
replace reelected = 1 if constituencynumber == 48021
replace reelected = 1 if constituencynumber == 48022
replace reelected = 1 if constituencynumber == 48023
replace reelected = 1 if constituencynumber == 48024
replace reelected = 1 if constituencynumber == 48025
replace reelected = 1 if constituencynumber == 48026
replace reelected = 1 if constituencynumber == 48027
replace reelected = 1 if constituencynumber == 48028
replace reelected = 1 if constituencynumber == 48029
replace reelected = 1 if constituencynumber == 48030
replace reelected = 1 if constituencynumber == 48031
replace reelected = 1 if constituencynumber == 48032
replace reelected = 1 if constituencynumber == 48033
replace reelected = 1 if constituencynumber == 48034
replace reelected = 1 if constituencynumber == 59001
replace reelected = 1 if constituencynumber == 59002
replace reelected = 1 if constituencynumber == 59003
replace reelected = 1 if constituencynumber == 59004
replace reelected = 1 if constituencynumber == 59005
replace reelected = 1 if constituencynumber == 59006
replace reelected = 0 if constituencynumber == 59007
replace reelected = 1 if constituencynumber == 59008
replace reelected = 1 if constituencynumber == 59009
replace reelected = 1 if constituencynumber == 59010
replace reelected = 1 if constituencynumber == 59011
replace reelected = 1 if constituencynumber == 59012
replace reelected = 1 if constituencynumber == 59013
replace reelected = 0 if constituencynumber == 59014
replace reelected = 0 if constituencynumber == 59015
replace reelected = 1 if constituencynumber == 59016
replace reelected = 0 if constituencynumber == 59017
replace reelected = 0 if constituencynumber == 59018
replace reelected = 1 if constituencynumber == 59019
replace reelected = 1 if constituencynumber == 59020
replace reelected = 1 if constituencynumber == 59021
replace reelected = 0 if constituencynumber == 59022
replace reelected = 0 if constituencynumber == 59023
replace reelected = 1 if constituencynumber == 59024
replace reelected = 1 if constituencynumber == 59025
replace reelected = 1 if constituencynumber == 59026
replace reelected = 0 if constituencynumber == 59027
replace reelected = 1 if constituencynumber == 59028
replace reelected = 1 if constituencynumber == 59029
replace reelected = 1 if constituencynumber == 59030
replace reelected = 0 if constituencynumber == 59031
replace reelected = 1 if constituencynumber == 59032
replace reelected = 1 if constituencynumber == 59033
replace reelected = 1 if constituencynumber == 59034
replace reelected = 1 if constituencynumber == 59035
replace reelected = 0 if constituencynumber == 59036
replace reelected = 1 if constituencynumber == 59037
replace reelected = 1 if constituencynumber == 59038
replace reelected = 1 if constituencynumber == 59039
replace reelected = 1 if constituencynumber == 59040
replace reelected = 1 if constituencynumber == 59041
replace reelected = 1 if constituencynumber == 59042
replace reelected = 1 if constituencynumber == 60001
replace reelected = 1 if constituencynumber == 61001
replace reelected = 0 if constituencynumber == 62001


***************************
** INDEPENDENT VARIABLES **
***************************

* Time

gen surveydate = dofc(cps19_EndDate)

format %tdMon_DD,_CCYY surveydate

gen edate = 20191021

gen electiondate=date(string(edate,"%8.0f"),"YMD") + 1

format %tdMon_DD,_CCYY electiondate

gen time = (electiondate - surveydate) 

replace time = . if time < 0

* Age

gen age = 2019 - (1919 + cps19_yob)

* Gender

gen male = cps19_gender

replace male = . if male == 3
replace male = 0 if male == 2

* Education

gen education = cps19_education

replace education = . if education == 12

* Interest for election

gen interest = cps19_interest_elxn

* Party identification (national)

gen pidstatus_whole = cps19_fed_id

replace pidstatus_whole = 2 if cps19_fed_id == 8
replace pidstatus_whole = 3 if cps19_fed_id == 1
replace pidstatus_whole = 1 if cps19_fed_id!=1 & cps19_fed_id!=.

replace pidstatus_whole = . if cps19_fed_id == 9

label define pidstatus 1 "Loser PID" 2 "No PID" 3 "Winner PID" 
label values pidstatus_whole pidstatus

gen winner_whole = 1 if pidstatus_whole == 3
replace winner_whole = 0 if pidstatus_whole!=3 & pidstatus_whole!=.

label define winner_whole 0 "Others" 1 "Winners"
label values winner_whole winner_whole

* Party identification (district)

gen pidstatus_district = cps19_fed_id

replace pidstatus_district = 2 if cps19_fed_id == 8
replace pidstatus_district = 3 if pidstatus_district == district_outcome & cps19_fed_id!=.
replace pidstatus_district = 1 if pidstatus_district!=district_outcome & cps19_fed_id!=.

replace pidstatus_district = . if cps19_fed_id == 9

label values pidstatus_district pidstatus

gen winner_district = 1 if pidstatus_district == 3
replace winner_district = 0 if pidstatus_district!=3 & pidstatus_district!=.

label define winner_district 0 "Others" 1 "Winners"
label values winner_district winner_district

* PID strength

gen strength = cps19_fed_id_str

replace strength = . if strength == 4

* Party identification scale (national)

gen pidscale_whole = .

replace pidscale_whole = 1 if pidstatus_whole == 1 & strength == 1
replace pidscale_whole = 2 if pidstatus_whole == 1 & strength == 2
replace pidscale_whole = 3 if pidstatus_whole == 1 & strength == 3
replace pidscale_whole = 4 if pidstatus_whole == 2
replace pidscale_whole = 5 if pidstatus_whole == 3 & strength == 3
replace pidscale_whole = 6 if pidstatus_whole == 3 & strength == 2
replace pidscale_whole = 7 if pidstatus_whole == 3 & strength == 1

label define pidscale 1 "Strong loser PID" 2 "Moderate loser PID" 3 "Weak loser PID" 4 "No PID" 5 "Weak winner PID" 6 "Moderate winner PID" 7 "Strong winner PID" 
label values pidscale_whole pidscale

* Party identification scale (district)

gen pidscale_district = .

replace pidscale_district = 1 if pidstatus_district == 1 & strength == 1
replace pidscale_district = 2 if pidstatus_district == 1 & strength == 2
replace pidscale_district = 3 if pidstatus_district == 1 & strength == 3
replace pidscale_district = 4 if pidstatus_district == 2
replace pidscale_district = 5 if pidstatus_district == 3 & strength == 3
replace pidscale_district = 6 if pidstatus_district == 3 & strength == 2
replace pidscale_district = 7 if pidstatus_district == 3 & strength == 1

label values pidscale_district pidscale

* Number of family members and relatives

gen relatives = mongrain_q5

replace relatives = . if relatives == 9

replace relatives = relatives - 1

* Number of close friends

gen friends = mongrain_q6

replace friends = . if friends == 9

replace friends = friends - 1

* Network size

gen size = relatives + friends

gen size_3pts = size
replace size_3pts = 0 if size == 1
replace size_3pts = 1 if size == 2
replace size_3pts = 1 if size == 3
replace size_3pts = 1 if size == 4
replace size_3pts = 2 if size > 4 & size!=.

* Political discussion with family and friends

gen discussion_family_friends = cps19_disc_fam_frien

replace discussion_family_friends = pes19_discfam if discussion_family_friends == .
replace discussion_family_friends = . if discussion_family_friends == 5

replace discussion_family_friends = discussion_family_friends - 1

gen discussion_family_friends_3pts = discussion_family_friends

replace discussion_family_friends_3pts = 2 if discussion_family_friends == 3

* Political discussion with people at work or school

gen discussion_work_school = cps19_disc_work_sch

replace discussion_work_school = pes19_discwork if discussion_work_school == .
replace discussion_work_school = . if discussion_work_school == 5

replace discussion_work_school = discussion_work_school - 1

gen discussion_work_school_3pts = discussion_work_school 

replace discussion_work_school_3pts = 2 if discussion_work_school == 3

* Political discussion frequency

gen discussion = discussion_family_friends_3pts + discussion_work_school_3pts

egen discussion_3pts = rowmax(discussion_family_friends_3pts discussion_work_school_3pts)

* Disagreement

gen disagreement = cps19_disc_disagree

replace disagreement = pes19_disagreed if disagreement == .
replace disagreement = . if disagreement == 5

replace disagreement = disagreement - 1

gen disagreement_3pts = disagreement

replace disagreement_3pts = 2 if disagreement == 3


****************************************
** NATIONAL-LEVEL EXPECTATIONS: SEATS **
****************************************

gen forecast_whole = mongrain_q1
replace forecast_whole = . if forecast_whole == 9

gen correct_whole_d = 0 if forecast_whole!=1
replace correct_whole_d = 1 if forecast_whole == 1

egen mean_correct_whole_d = mean(correct_whole_d)


******************************************************
** NATIONAL-LEVEL EXPECTATIONS: SEATS (BRIER SCORE) **
******************************************************

* Generate sum of expectations

gen lpc_chance_whole = cps19_most_seats_1
gen cpc_chance_whole = cps19_most_seats_2
gen ndp_chance_whole = cps19_most_seats_3
gen bq_chance_whole = cps19_most_seats_4
gen gpc_chance_whole = cps19_most_seats_5
gen ppc_chance_whole = cps19_most_seats_6

gen complete_whole = 1 if lpc_chance_whole!=. & cpc_chance_whole!=. & ndp_chance_whole!=. & bq_chance_whole!=. & gpc_chance_whole!=. & ppc_chance_whole!=.

gen sum_whole = lpc_chance_whole + cpc_chance_whole + ndp_chance_whole + bq_chance_whole + gpc_chance_whole + ppc_chance_whole

* Generate normalized expectations

gen lpc_cbs_whole = lpc_chance_whole/sum_whole if complete_whole == 1
gen cpc_cbs_whole = cpc_chance_whole/sum_whole if complete_whole == 1
gen ndp_cbs_whole = ndp_chance_whole/sum_whole if complete_whole == 1
gen bq_cbs_whole = bq_chance_whole/sum_whole if complete_whole == 1
gen gpc_cbs_whole = gpc_chance_whole/sum_whole if complete_whole == 1
gen ppc_cbs_whole = ppc_chance_whole/sum_whole if complete_whole == 1

replace lpc_cbs_whole = 0 if sum_whole == 0 & complete_whole == 1
replace cpc_cbs_whole = 0 if sum_whole == 0 & complete_whole == 1
replace ndp_cbs_whole = 0 if sum_whole == 0 & complete_whole == 1
replace bq_cbs_whole = 0 if sum_whole == 0 & complete_whole == 1
replace gpc_cbs_whole = 0 if sum_whole == 0 & complete_whole == 1
replace ppc_cbs_whole = 0 if sum_whole == 0 & complete_whole == 1

* Generate cumulative Brier score index

gen cbs_whole = .

replace cbs_whole = 1 - ((lpc_cbs_whole - 1)^2 + (cpc_cbs_whole - 0)^2 + (ndp_cbs_whole - 0)^2 + (bq_cbs_whole - 0)^2 + (gpc_cbs_whole - 0)^2 + (ppc_cbs_whole - 0)^2) if complete_whole == 1

egen mean_cbs_whole = mean(cbs_whole)


************************************************************
** NATIONAL-LEVEL EXPECTATIONS: LIBERAL MINORITY/MAJORITY **
************************************************************

gen forecast_minority = mongrain_q2
replace forecast_minority = . if forecast_minority == 5

gen correct_lpc_minority_d = 1 if forecast_minority == 1 & correct_whole_d == 1
replace correct_lpc_minority_d = 0 if forecast_minority!=1 | correct_whole_d == 0

egen mean_correct_lpc_minority_d = mean(correct_lpc_minority_d)


****************************************************
** NATIONAL-LEVEL EXPECTATIONS: MINORITY/MAJORITY **
****************************************************

gen correct_minority_d = 1 if forecast_minority == 1
replace correct_minority_d = 0 if forecast_minority!=1

egen mean_correct_minority_d = mean(correct_minority_d)


*****************************************************
** NATIONAL-LEVEL EXPECTATIONS: LIBERAL VOTE SHARE **
*****************************************************

gen forecast_vote = mongrain_q3
replace forecast_vote = . if forecast_vote < 18 
replace forecast_vote = . if forecast_vote > 61

gen vote_share = abs(forecast_vote - 33.07)

egen mean_vote_share = mean(vote_share)


*********************************
** DISTRICT-LEVEL EXPECTATIONS **
*********************************

gen forecast_district = mongrain_q4
replace forecast_district = . if forecast_district == 9

gen correct_district_d = 0 if forecast_district!=district_outcome
replace correct_district_d = 1 if forecast_district == district_outcome

egen mean_correct_district_d = mean(correct_district_d)


***********************************************
** DISTRICT-LEVEL EXPECTATIONS (BRIER SCORE) **
***********************************************

* Generate sum of expectations

gen lpc_chance_district = cps19_win_local_1
gen cpc_chance_district = cps19_win_local_2
gen ndp_chance_district = cps19_win_local_3
gen bq_chance_district = cps19_win_local_4
gen gpc_chance_district = cps19_win_local_5
gen ppc_chance_district = cps19_win_local_6

gen complete_district_roc = 1 if lpc_chance_district!=. & cpc_chance_district!=. & ndp_chance_district!=. & gpc_chance_district!=. & ppc_chance_district!=. & cps19_province!=24

gen complete_district_qc = 1 if lpc_chance_district!=. & cpc_chance_district!=. & ndp_chance_district!=. & bq_chance_district!=. & gpc_chance_district!=. & ppc_chance_district!=. & cps19_province == 24

gen complete_district = max(complete_district_roc, complete_district_qc)

gen sum_district_roc = lpc_chance_district + cpc_chance_district + ndp_chance_district + gpc_chance_district + ppc_chance_district if cps19_province!=24

gen sum_district_bq = lpc_chance_district + cpc_chance_district + ndp_chance_district + bq_chance_district + gpc_chance_district + ppc_chance_district if cps19_province == 24

gen sum_district = max(sum_district_roc, sum_district_bq)

* Generate normalized expectations

gen lpc_cbs_district = lpc_chance_district/sum_district if complete_district == 1
gen cpc_cbs_district = cpc_chance_district/sum_district if complete_district == 1
gen ndp_cbs_district = ndp_chance_district/sum_district if complete_district == 1
gen bq_cbs_district = bq_chance_district/sum_district if complete_district == 1 & cps19_province == 24
gen gpc_cbs_district = gpc_chance_district/sum_district if complete_district == 1
gen ppc_cbs_district = ppc_chance_district/sum_district if complete_district == 1

replace lpc_cbs_district = 0 if sum_district == 0 & complete_district == 1
replace cpc_cbs_district = 0 if sum_district == 0 & complete_district == 1
replace ndp_cbs_district = 0 if sum_district == 0 & complete_district == 1
replace bq_cbs_district = 0 if sum_district == 0 & complete_district == 1 & cps19_province == 24
replace gpc_cbs_district = 0 if sum_district == 0 & complete_district == 1
replace ppc_cbs_district = 0 if sum_district == 0 & complete_district == 1

* Generate cumulative Brier score index

gen cbs_district = .

replace cbs_district = 1 - ((lpc_cbs_district - 1)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 0)^2 + (bq_cbs_district - 0)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 1 & cps19_province == 24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 1)^2 + (ndp_cbs_district - 0)^2 + (bq_cbs_district - 0)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 2 & cps19_province == 24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 1)^2 + (bq_cbs_district - 0)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 3 & cps19_province == 24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 0)^2 + (bq_cbs_district - 1)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 4 & cps19_province == 24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 0)^2 + (bq_cbs_district - 0)^2 + (gpc_cbs_district - 1)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 5 & cps19_province == 24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 0)^2 + (bq_cbs_district - 0)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 1)^2) if complete_district == 1 & district_outcome == 6 & cps19_province == 24

replace cbs_district = 1 - ((lpc_cbs_district - 1)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 0)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 1 & cps19_province!=24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 1)^2 + (ndp_cbs_district - 0)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 2 & cps19_province!=24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 1)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 3 & cps19_province!=24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 0)^2 + (gpc_cbs_district - 1)^2 + (ppc_cbs_district - 0)^2) if complete_district == 1 & district_outcome == 5 & cps19_province!=24
replace cbs_district = 1 - ((lpc_cbs_district - 0)^2 + (cpc_cbs_district - 0)^2 + (ndp_cbs_district - 0)^2 + (gpc_cbs_district - 0)^2 + (ppc_cbs_district - 1)^2) if complete_district == 1 & district_outcome == 6 & cps19_province!=24

egen mean_cbs_district = mean(cbs_district)


**********
** SAVE **
**********

save "ca2019_ces.dta", replace


*******************
** MAIN ANALYSES **
*******************

* Regression analysis and proportion of correct and incorrect forecasts (national: most seats)

logistic correct_whole_d discussion disagreement size pidscale_whole interest age i.male education
estimates table, star(.05 .01 .001)

margins, at(pidscale_whole=1) atmeans predict(pr) // Average marginal effects
margins, at(pidscale_whole=7) atmeans predict(pr) // Average marginal effects

tab correct_whole_d if e(sample) == 1

* Regression analysis (national: most seats, CBS)

reg cbs_whole discussion disagreement size pidscale_whole interest education age i.male
estimates table, star(.05 .01 .001)

tab cbs_whole if e(sample) == 1

* Regression analysis and proportion of correct and incorrect forecasts (national: minority)

logistic correct_lpc_minority_d discussion disagreement size pidscale_whole interest age i.male education
estimates table, star(.05 .01 .001)

tab correct_lpc_minority_d if e(sample) == 1

* Regression analysis and mean absolute error (national: liberal vote share)

reg vote_share discussion disagreement size pidscale_whole interest age i.male education
estimates table, star(.05 .01 .001)

tab vote_share if e(sample) == 1

* Regression analysis and proportion of correct and incorrect forecasts (district)

logistic correct_district_d discussion disagreement size pidscale_district interest age i.male education, cluster(constituencynumber)
estimates table, star(.05 .01 .001)

margins, at(pidscale_district=1) atmeans predict(pr) // Average marginal effects
margins, at(pidscale_district=7) atmeans predict(pr) // Average marginal effects

tab correct_whole_d if e(sample) == 1

* Regression analysis (district, CBS)

reg cbs_district discussion disagreement size pidscale_district interest age i.male education, cluster(constituencynumber)
estimates table, star(.05 .01 .001)

tab cbs_district if e(sample) == 1


************************
** SUMMARY STATISTICS **
************************

label variable vote_share "Vote share (DV)"
label variable correct_whole_d "Most seats (DV)"
label variable correct_lpc_minority_d "Minority or majority (DV)"
label variable correct_district_d "District winner (DV)"
label variable cbs_whole "Most seats CBS"
label variable cbs_district "District CBS"
label variable discussion "Discussion"
label variable disagreement "Disagreement"
label variable size "Network size"
label variable pidscale_whole "National PID"
label variable pidscale_district "District PID"
label variable interest "Interest"
label variable education "Education"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable enep "ENEP"
label variable margin "Margin"
label variable reelected "Reelected"

quietly logistic correct_whole_d discussion disagreement size pidscale_whole interest age i.male education

estpost tabstat correct_whole_d discussion disagreement size pidscale_whole interest age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "ca2019_ces_summary_mostseats.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2019 Canadian federal election (CES) (most seats), national level") replace

eststo clear

quietly logistic correct_lpc_minority_d discussion disagreement size pidscale_whole interest age i.male education

estpost tabstat correct_lpc_minority_d discussion disagreement size pidscale_whole interest age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "ca2019_ces_summary_majority.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2019 Canadian federal election (CES) (Liberal minority or majority), national level") replace

eststo clear

quietly melogit correct_district_d discussion disagreement size pidscale_district interest age i.male education enep i.reelected || constituencynumber: , or

estpost tabstat correct_district_d discussion disagreement size pidscale_district interest age male education if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "ca2019_ces_summary_district.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2019 Canadian federal election (CES), district level") replace

eststo clear

log close